import wepy from 'wepy'

// 公共基础函数
export const getParamsMonth = () => {
    let now = new Date()
    let year = now.getFullYear()
    let Month = now.getMonth() + 1

    return `${year}-${formatNumber(Month)}`
}

export const formatNumber = num => {
    return +num > 9 ? num : '0'+ num
}

export const today = () => {
    let now = new Date()
    let year = now.getFullYear()
    let Month = formatNumber(now.getMonth() + 1)
    let day = formatNumber(now.getDate())

    let group = [ year, Month, day]

    return group.join('-')
}

// 具体业务逻辑
// 请求数据

export const getCalender = month => wepy.request(`/moment/calendar?month=${month}`)
export const getCourseData = () => wepy.request(`/desk`)
// export const getCalender = () => calenderData.data

// export const getCourseData = () => courseData.data

// 对后端数据进行信任排序-增加需要字段
export const processData = data => {
    let todayI = today()
    data.forEach( x => {

        let xGroup = x.day.split('-')

        if(x.day == todayI){
            x.text = '今'
        } else {
            x.text = +xGroup[2]
        }

        if(x.record_count === 1){
            x.type = 'hasWrite'
        } else {
            if(x.day > todayI){
                x.type = "canNotDo"
            } else {
                x.type = "noWrite"
            }
        }
    })

    return data
}

export const addMonth = label => {
    let [ year, month ] = label.split('-')

    if(+month < 12){
        +month++
    } else {
        year++
        month = 1
    }

    return `${year}-${formatNumber(month)}`
}

export const minusMonth = label => {
    let [ year, month ] = label.split('-')

    if(+month > 2){
        +month--
    } else {
        year--
        month = 12
    }

    return `${year}-${formatNumber(month)}`
}

export const get_first_month = data => {
    return data[0].first_month
}
/**
 * 对不同的点击事件做不同的处
 * @params type(1 已写 2 未写 3 时间未到)
 *
 **/
export const calenderClick = (item,id,TopcourseId) => {
    // 处理linkId
    if(item.link_lesson) {
        wx.setStorage({
            key: 'linkId',
            data: item.link_lesson.lesson_id
        })
      wx.setStorage({
        key: 'getWayIdd',
        data: item.link_lesson.lesson_id
      })
    } else {
        wx.setStorage({
            key: 'linkId',
            data: "no_link"
        })
    }

    switch(item.type){
        case "noWrite":
            // jump to hasWrite
            wx.setStorage({
                key: 'backPath',
                data: `course-detail?id=${id}`
              })
              wx.setStorage({
                key: 'momentAdd',
                data: `0`
              })
              wx.setStorage({
                key: 'courseId',
                data: TopcourseId
              })
              const createTime = +new Date(item.day)/1000
            break;
        case "hasWrite":
            break;
        default:
            // do nothing
            wepy.showToast({
                title: '还没有到这一天哦~',
                icon: 'none',
                duration: 1200
            })

            break;
    }
}



const calenderData =  {
    "code": 200,
    "message": "ok",
    "data": [
        {
            "day": "2018-08-01",
            "record_count": 0
        },
        {
            "day": "2018-08-02",
            "record_count": 1   //如果大于0，则表示有记录，记录条数为这个值
        },
        {
            "day": "2018-08-03",
            "record_count": 0
        },
        {
            "day": "2018-08-04",
            "record_count": 0
        },
        {
            "day": "2018-08-05",
            "record_count": 0
        },
        {
            "day": "2018-08-06",
            "record_count": 0
        },
        {
            "day": "2018-08-07",
            "record_count": 0
        },
        {
            "day": "2018-08-08",
            "record_count": 0
        },
        {
            "day": "2018-08-09",
            "record_count": 0
        },
        {
            "day": "2018-08-10",
            "record_count": 0
        },
        {
            "day": "2018-08-11",
            "record_count": 0
        },
        {
            "day": "2018-08-12",
            "record_count": 0
        },
        {
            "day": "2018-08-13",
            "record_count": 0
        },
        {
            "day": "2018-08-14",
            "record_count": 0
        },
        {
            "day": "2018-08-15",
            "record_count": 0
        },
        {
            "day": "2018-08-16",
            "record_count": 0
        },
        {
            "day": "2018-08-17",
            "record_count": 0
        },
        {
            "day": "2018-08-18",
            "record_count": 0
        },
        {
            "day": "2018-08-19",
            "record_count": 0
        },
        {
            "day": "2018-08-20",
            "record_count": 0
        },
        {
            "day": "2018-08-21",
            "record_count": 0
        },
        {
            "day": "2018-08-22",
            "record_count": 0
        },
        {
            "day": "2018-08-23",
            "record_count": 0
        },
        {
            "day": "2018-08-24",
            "record_count": 0
        },
        {
            "day": "2018-08-25",
            "record_count": 0
        },
        {
            "day": "2018-08-26",
            "record_count": 0
        },
        {
            "day": "2018-08-27",
            "record_count": 0
        },
        {
            "day": "2018-08-28",
            "record_count": 0
        },
        {
            "day": "2018-08-29",
            "record_count": 0
        },
        {
            "day": "2018-08-30",
            "record_count": 0
        },
        {
            "day": "2018-08-31",
            "record_count": 0
        }
    ]
}

export const courseData = {
    "code": 200,
    "message": "ok",
    "data": [
        {
            "uid": 3,
            "course_id": 1,
            "is_finish": 1,
            "schedule": 73,  //进度
            "id": 1,
            "name": "亲子阅读行动团",
            "subname": "28天养成亲子英文阅读好习惯",
            "category_id": 1,
            "cover_url": "https://image.snaily.com.cn/WechatIMG1523.jpeg",
            "original_price": "199.00",
            "present_price": "99.00",
            "tag": null,
            "on_line": 1,
            "create_time": 1532256931,
            "update_time": null,
            "lesson_count": 3,  //课时总数
            "task_nums": 16,    //日记总数
            "is_buy": 1,        //是否购买
            "complete_course": 8, //已经完成课时
            "surplus_course": 8   //未完成课时
        },
        {
            "uid": 3,
            "course_id": 1,
            "is_finish": 1,
            "schedule": 73,  //进度
            "id": 1,
            "name": "亲子阅读行动团",
            "subname": "28天养成亲子英文阅读好习惯",
            "category_id": 1,
            "cover_url": "https://image.snaily.com.cn/WechatIMG1523.jpeg",
            "original_price": "199.00",
            "present_price": "99.00",
            "tag": null,
            "on_line": 1,
            "create_time": 1532256931,
            "update_time": null,
            "lesson_count": 3,  //课时总数
            "task_nums": 16,    //日记总数
            "is_buy": 1,        //是否购买
            "complete_course": 8, //已经完成课时
            "surplus_course": 8   //未完成课时
        }
    ]
}
